package com.quwan.job;


import com.quwan.common.enums.JobStatusEnum;
import com.quwan.common.enums.JobTypeEnum;
import com.quwan.entity.JobRunStateInfo;
import com.quwan.mapper.JobRunStateInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;


/**
 * @author quan
 * @date 2021-07-27 15:11
 */

public abstract class AbstractJobs {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    protected String name = "";
    protected String code = "";
    private JobRunStateInfoMapper jobRunStateInfoMapper;

    public AbstractJobs(@NotNull JobRunStateInfoMapper jobRunStateInfoMapper) {
        this.jobRunStateInfoMapper = jobRunStateInfoMapper;
    }

    public void Execute() {
        name = "推广数据统计";
        code = getClass().getSimpleName();
        logger.debug("process[" + name + "]begin");
        long b_t = System.currentTimeMillis();

        try {
            String message = ExecuteTask();
            logger.debug(message);
            updateRunState(JobStatusEnum.SUCEESS.getCode(), message, "", (System.currentTimeMillis() - b_t),JobTypeEnum.PROMOTE.getCode());
        } catch (Exception ex) {
            logger.error("process[" + name + "]exception", ex);
            updateRunState(JobStatusEnum.FAIL.getCode(), ex.getMessage(), "", (System.currentTimeMillis() - b_t),JobTypeEnum.PROMOTE.getCode());

        }
        logger.debug("process[" + name + "]finish，handle time：" + (System.currentTimeMillis() - b_t));

    }

    public void preserveDataExecute() {
        code = getClass().getSimpleName();
        name = "留存报表数据统计";
        logger.debug("process[" + name + "]begin");
        long b_t = System.currentTimeMillis();

        try {
            String message = preserveDataExecuteTask();
            logger.debug(message);
            updateRunState(JobStatusEnum.SUCEESS.getCode(), message, "", (System.currentTimeMillis() - b_t),JobTypeEnum.RETAINED.getCode());
        } catch (Exception ex) {
            logger.error("process[" + name + "]exception", ex);
            updateRunState(JobStatusEnum.FAIL.getCode(), ex.getMessage(), "", (System.currentTimeMillis() - b_t),JobTypeEnum.RETAINED.getCode());

        }
        logger.debug("process[" + name + "]finish，handle time：" + (System.currentTimeMillis() - b_t));

    }

    public void syncChatMsgExecute() {
        code = getClass().getSimpleName();
        name = "同步聊天消息记录";
        logger.debug("process[" + name + "]begin");
        long b_t = System.currentTimeMillis();

        try {
            String message = syncChatMsgExecuteTask();
            logger.debug(message);
            updateRunState(JobStatusEnum.SUCEESS.getCode(), message, "", (System.currentTimeMillis() - b_t),JobTypeEnum.MSG_SYNC.getCode());
        } catch (Exception ex) {
            logger.error("process[" + name + "]exception", ex);
            updateRunState(JobStatusEnum.FAIL.getCode(), ex.getMessage(), "", (System.currentTimeMillis() - b_t),JobTypeEnum.MSG_SYNC.getCode());

        }
        logger.debug("process[" + name + "]finish，handle time：" + (System.currentTimeMillis() - b_t));

    }

    public void financeDataExecute() {
        code = getClass().getSimpleName();
        name = "财务报表数据统计";
        logger.debug("process[" + name + "]begin");
        long b_t = System.currentTimeMillis();

        try {
            String message = financeDataExecuteTask();
            logger.debug(message);
            updateRunState(JobStatusEnum.SUCEESS.getCode(), message, "", (System.currentTimeMillis() - b_t),JobTypeEnum.FINANCE.getCode());
        } catch (Exception ex) {
            logger.error("process[" + name + "]exception", ex);
            updateRunState(JobStatusEnum.FAIL.getCode(), ex.getMessage(), "", (System.currentTimeMillis() - b_t),JobTypeEnum.FINANCE.getCode());

        }
        logger.debug("process[" + name + "]finish，handle time：" + (System.currentTimeMillis() - b_t));

    }


    protected abstract String ExecuteTask() throws Exception;
    protected abstract String preserveDataExecuteTask() throws Exception;
    protected abstract String financeDataExecuteTask() throws Exception;
    protected abstract String syncChatMsgExecuteTask() throws Exception;

    private void updateRunState(byte run_state, String message, String remark, Long useTime,byte type) {
        message = checkString(message, 255);
        remark = checkString(remark, 255);
        if (jobRunStateInfoMapper.insert(
                new JobRunStateInfo()
                        .setCode(code)
                        .setType(type)
                        .setName(name)
                        .setLastRunTime(LocalDateTime.now())
                        .setLastRunUseTime(Integer.valueOf(useTime + ""))
                        .setLastRunState(run_state)
                        .setLastRunMessage(message)
                        .setRemark(remark)) != 1) {
            logger.error("insert jobRunStateInfo err  code::{}", code);

        } else {
            logger.debug("insert jobRunStateInfo  success");
        }


    }

    private String checkString(String s, int len) {
        return s.length() > len ? s.substring(0, len) : s;
    }


}
